//
//  MBProgressHUD+Tool.m
//  PaasLocal
//
//  Created by 余意 on 2018/7/2.
//  Copyright © 2018年 余意. All rights reserved.
//

#import "MBProgressHUD+Tool.h"

@implementation MBProgressHUD (Tool)

+ (void)loadingView:(UIView *)view
{
    if (!view)
    {
        view = [self lastWindow];
    }
    
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.userInteractionEnabled = YES;
    hud.removeFromSuperViewOnHide = YES;
    
}

+ (void)loading
{
    [self loadingView:nil];
}

+ (void)showMessage:(NSString *)message
             toView:(UIView *)view
         afterDelay:(double)second
  WithCompleteBlock:(void (^)(void))completeBlock
{
    if (!view)
    {
        view = [self lastWindow];
    }
    
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.detailsLabel.text = message;
    hud.detailsLabel.font = [UIFont systemFontOfSize:16];
    hud.mode = MBProgressHUDModeText;
    hud.animationType = MBProgressHUDAnimationZoom;
    hud.removeFromSuperViewOnHide = YES;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [hud hideAnimated:YES afterDelay:second];
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(second * NSEC_PER_SEC));
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            completeBlock();
        });
    });
}

+ (void)showMessage:(NSString *)message toView:(UIView *)view afterDelay:(double)second
{
    if (!view)
    {
        view = [self lastWindow];
    }
    
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.detailsLabel.text = message;
    hud.detailsLabel.font = [UIFont systemFontOfSize:16];
    hud.mode = MBProgressHUDModeText;
    hud.animationType = MBProgressHUDAnimationZoom;
    hud.removeFromSuperViewOnHide = YES;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [hud hideAnimated:YES afterDelay:second];
    });
}

+ (void)showMessage:(NSString *)message toView:(UIView *)view
{
    [self showMessage:message toView:view afterDelay:2.f];
}

+ (void)showMessage:(NSString *)message
{
    [self showMessage:message toView:nil];
}

+ (void)hiddenFromView:(UIView *)view
{
    if (!view)
    {
        view = [self lastWindow];
    }
    
    [self hideHUDForView:view animated:YES];
    //    dispatch_async(dispatch_get_main_queue(), ^{
    //
    //    });
}

+ (void)hiddenHUD
{
    [self hiddenFromView:nil];
}


#pragma mark - 获取视图
//+ (UIView *)lastWindow
//{
//    return [UIApplication sharedApplication].delegate.window;
//}

+ (UIView *)lastWindow
{
    return [self currentViewController].view;
}

+ (UIViewController *)currentViewController
{
    UIViewController * rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
    return [self currentViewControllerFromRootViewController:rootViewController];
}

+ (UIViewController *)currentViewControllerFromRootViewController:(UIViewController *)rootVC
{
    UIViewController * currentVC;
    if ([rootVC presentedViewController])
    {
        //vc是被presented出来的
        rootVC = [rootVC presentedViewController];
    }
    
    if ([rootVC isKindOfClass:[UITabBarController class]])
    {
        //跟视图是UITabbarController
        currentVC = [self currentViewControllerFromRootViewController:[(UITabBarController *)rootVC selectedViewController]];
    }
    else if ([rootVC isKindOfClass:[UINavigationController class]])
    {
        //跟视图是UINavigationController
        currentVC = [self currentViewControllerFromRootViewController:[(UINavigationController *)rootVC visibleViewController]];
    }
    else
    {
        //跟视图不是导航类
        currentVC = rootVC;
    }
    
    return currentVC;
    
}

@end
